﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.DynamicData;
using System.Web.SessionState;

namespace NotAClue.Web
{
    public static class SessionExtensionMethods
    {
        ///// <summary>
        ///// Adds the custom filter's current value to the session state.
        ///// </summary>
        ///// <param name="page">The page.</param>
        ///// <param name="key">The table.</param>
        ///// <param name="pageField">The page field.</param>
        ///// <param name="value">The value.</param>
        //public static void AddValueToSession<T>(this Page page, String key, T value)
        //{
        //    var obj = page.Session[key];

        //    if (obj != null)
        //        page.Session[key] = value;
        //    else
        //        page.Session.Add(key, value);
        //}

        ///// <summary>
        ///// Gets the filter value from session.
        ///// </summary>
        ///// <param name="page">The page.</param>
        ///// <param name="column">The column.</param>
        ///// <returns></returns>
        //public static T GetValueFromSession<T>(this Page page, String key)
        //{
        //    var obj = page.Session[key];

        //    if (obj != null && obj is T)
        //        return (T)obj;

        //    return default(T);
        //}

        /// <summary>
        /// Adds a value 'T' to session with key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page">The page.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        public static void AddValueToSession<T>(this Page page, String key, T value)
        {
            page.Session.AddValueToSession<T>(key, value);
        }

        /// <summary>
        /// Adds to session.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session">The session.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        public static void AddValueToSession<T>(this HttpSessionState session, String key, T value)
        {
            var obj = session[key];

            if (obj != null)
                session[key] = value;
            else
                session.Add(key, value);
        }

        /// <summary>
        /// Gets the value 'T' value from the session using key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page">The page.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static T GetValueFromSession<T>(this Page page, String key)
        {
            return page.Session.GetValueFromSession<T>(key);
        }

        /// <summary>
        /// Gets the value 'T' from the session using the key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session">The session.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static T GetValueFromSession<T>(this HttpSessionState session, String key)
        {
            var obj = session[key];

            if (obj != null && obj is T)
                return (T)obj;

            return default(T);
        }
    }
}
